package com.wjy.rbac.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjy.rbac.bean.RequestMethodBean;
import com.wjy.rbac.entity.RequestMethod;
import com.wjy.rbac.entity.RequestMethodB;
import com.wjy.rbac.mapper.RequestMethodMapper;
import com.wjy.rbac.service.IRequestMethodBService;
import com.wjy.rbac.service.IRequestMethodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ybxxszl
 * @since 2019-04-12
 */
@Service
public class RequestMethodServiceImpl extends ServiceImpl<RequestMethodMapper, RequestMethod>
        implements IRequestMethodService {

    @Autowired
    @Qualifier(value = "requestMethodBServiceImpl")
    private IRequestMethodBService requestMethodBService;

    @Override
    public void insert(RequestMethodBean requestMethodBean) {
        RequestMethod requestMethod = requestMethodBean.getRequestMethod();
        requestMethod.setStatus(0);
        save(requestMethod);
        List<RequestMethodB> requestMethodBList = requestMethodBean.getRequestMethodBList();
        for (RequestMethodB requestMethodB : requestMethodBList) {
            requestMethodB.setRequestMethodId(requestMethod.getRequestMethodId());
        }
        requestMethodBService.insert(requestMethodBList);
    }

    @Override
    public void update(RequestMethodBean requestMethodBean) {
        RequestMethod requestMethod = requestMethodBean.getRequestMethod();
        requestMethod.setStatus(0);
        updateById(requestMethod);
        requestMethodBService.deleteByRequestMethodId(requestMethod.getRequestMethodId());
        List<RequestMethodB> requestMethodBList = requestMethodBean.getRequestMethodBList();
        requestMethodBService.insert(requestMethodBList);
    }

    @Override
    public void delete(String requestMethodId) {
        removeById(requestMethodId);
        requestMethodBService.deleteByRequestMethodId(requestMethodId);
    }

    @Override
    public Map<String, Object> get(String requestMethodId) {
        Map<String, Object> result = new HashMap<String, Object>();
        RequestMethod requestMethod = getById(requestMethodId);
        result.put("requestMethod", requestMethod);
        List<RequestMethodB> requestMethodBList = requestMethodBService.getByRequestMethodId(requestMethod.getRequestMethodId());
        result.put("requestMethodBList", requestMethodBList);
        return result;
    }

    @Override
    public List<Map<String, Object>> getByRequestMethodIdList(List<String> requestMethodIdList) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (String requestMethodId : requestMethodIdList) {
            result.add(get(requestMethodId));
        }
        return result;
    }

}
